package com.gxa.clientproduct.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientproduct.pojo.*;
import com.gxa.clientproduct.service.*;
import com.gxa.clientproduct.utils.CreateNumber;
import com.gxa.clientproduct.utils.JsonResult;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: fenglin
 * @Date:2020/11/11 14:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/product")
@Api(value = "执行商品管理的控制器",tags = "商品管理模块")
public class ProductController {
    @Resource
    ProductService productService=null;
    @Resource
    SpellpriceService spellpriceService=null;
    @Resource
    SpecsService specsService=null;
    @Resource
    ClassifyService classifyService;
    @Resource
    LableService lableService=null;
    @Resource
    CommentService commentService=null;



    JsonResult jsonResult=new JsonResult();

    String number = CreateNumber.getNum();

    @PostMapping("/add")
    @ApiOperation(value = "商品管理",notes = "用于添加商品",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @RequiresPermissions("2")
    public Map insert(@RequestBody @ApiParam(name = "product",value = "商品管理的pojo信息",required = true) Product product){



        product.setSysnum(number);

        int i = productService.productAdd(product);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

    @PostMapping("/spellpriceAdd")
    @ApiOperation(value = "商品管理",notes = "添加拼团价格",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map spellpriceAdd (@RequestBody @ApiParam(name = "spellprice",value = "商品管理添加拼团价格的pojo信息",required = true) Spellprice spellprice){

        spellprice.setSysnum(number);
        int i = spellpriceService.spellpriceAdd(spellprice);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }



    @PostMapping("/specsAdd")
    @ApiOperation(value = "商品管理",notes = "用于添加商品规格",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map specsAdd (@RequestBody @ApiParam(name = "specs",value = "商品规格的pojo信息",required = true) Specs specs){


        specs.setSysnum(number);
        int i = specsService.add(specs);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }


    @PostMapping("/update")
    @RequiresPermissions("3")
    @ApiOperation(value = "商品管理",notes = "修改商品信息",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map update (@RequestBody @ApiParam(name = "product",value = "商品管理的pojo信息",required = true) Product product){
        int i = productService.productUpdate(product);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

    @PostMapping("/state")
    @ApiOperation(value = "商品管理",notes = "修改商品状态（0/下架、1/上架、2/回收站）",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map state (@RequestBody @ApiParam(name = "Product",value = "商品管理的pojo信息",required = true) Product product){

      productService.state(product);

        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/findAll")
    @ApiOperation(value = "商品管理",notes = "用于查找所有商品信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query")
    })
    public Map findAll(@RequestParam("page") int page,@RequestParam("limit") int limit){

        Page<Object> pageinfo= PageHelper.startPage(page,limit);


        List<Product> all = productService.findAll();

        PageInfo<Product> managerPageInfo = new PageInfo<>(all);
        jsonResult.put("count",managerPageInfo.getTotal());
        jsonResult.setData(managerPageInfo.getList());

        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/findAllRecovery")
    @ApiOperation(value = "商品管理",notes = "用于查找所有查找回收信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query")
    })
    public Map findAllRecovery(@RequestParam("page") int page,@RequestParam("limit") int limit){

        Page<Object> pageinfo= PageHelper.startPage(page,limit);


        List<Product> all = productService.findAllRecovery();
        PageInfo<Product> managerPageInfo = new PageInfo<>(all);
        jsonResult.put("count",managerPageInfo.getTotal());
        jsonResult.setData(managerPageInfo.getList());

        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }


    @PostMapping("/findNameAndNum")
    @ApiOperation(value = "商品管理",notes = "用于查找商品信息的名称或货号",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "商品名称",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "num",value = "商品货号",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "classify",value = "商品货号",required = true,dataType = "Integer",paramType = "query")
    })
    public Map findNameAndNum(@RequestParam("page") int page,
                              @RequestParam("limit") int limit,
                              @RequestParam("name") String name,
                              @RequestParam("num") String num,
                               @RequestParam("classify") Integer classify  ){

        Page<Object> startPage = PageHelper.startPage(page, limit);
        List<Product> products = productService.fandNameAndNum(name, num,classify);
        PageInfo<Product> productPageInfo = new PageInfo<>(products);
        jsonResult.put("count",productPageInfo.getTotal());
        jsonResult.setData(productPageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }


    @PostMapping("/del")
    @ApiOperation(value = "商品管理",notes = "根据商品id删除",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "根据商品id删除",required = true,dataType = "int",paramType = "query")
    })
    public Map del(@RequestParam("id") int id){
        Product product = new Product();
        product.setId(id);

        int i = productService.del(product);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }


    @PostMapping("/classify/add")
    @RequiresPermissions("3")
    @ApiOperation(value = "商品分类的添加接口",notes = "添加分类信息",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map classifyInsert(@RequestBody @ApiParam(name = "classify",value = "系统用户的信息",required = true) Classify classify){

        int i = classifyService.classifyAdd(classify);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }


    @PostMapping("/classify/update")
    @RequiresPermissions("4")
    @ApiOperation(value = "商品分类的修改接口",notes = "商品分类的修改接口",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map classifyUpdate (@RequestBody @ApiParam(name = "classify",value = "排序或分类名称修改",required = true) Classify classify){

    classifyService.classifyUpdate(classify)  ;
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/classify/findAll")
    @ApiOperation(value = "商品分类的查找接口",notes = "查找所有的商品分类信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query")
    })
    public Map classifyFindAll(@RequestParam("page") int page, @RequestParam("limit") int limit){

        Page<Object> pageinfo= PageHelper.startPage(page,limit);
        List<Classify> classifies = classifyService.classifyFindAll();
        PageInfo<Classify> classifyPageInfo = new PageInfo<>(classifies);
        jsonResult.put("count",classifyPageInfo.getTotal());
        jsonResult.setData(classifyPageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }
    @PostMapping("/classify/state")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state",value = "状态是否显示（false/不显示、true/显示）",required = true,dataType = "Boolean",paramType = "query")
    })
    public Map classifyFindState (@RequestParam("state") Boolean state){

        List<Classify> classifies = classifyService.classifyFindState(state);
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/classify/del")
    @ApiOperation(value = "商品分类的删除接口",notes = "根据该数据当前的id删除",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "该数据当前的id",required = true,dataType = "int",paramType = "query")
    })
    public Map classifyDel(@RequestParam("id") int id){
        Classify classify = new Classify();
        classify.setId(id);
        int i = classifyService.classifyDel(classify);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

        //lable是便签管理

    @PostMapping("/lable/findAll")
    @ApiOperation(value = "商品标签的查找接口",notes = "查找所有的便签信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query")
    })
    public Map lableFindAll(@RequestParam int page, @RequestParam int limit){

        Page<Object> objects = PageHelper.startPage(page, limit);

        List<Lable> lables = lableService.lableFindAll();

        PageInfo<Lable> pageInfo = new PageInfo<>(lables);
        jsonResult.put("count",pageInfo.getTotal());
        jsonResult.setData(pageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/lable/add")
    @RequiresPermissions("5")
    @ApiOperation(value = "商品管理",notes = "用于添加商品",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map lableInsert(@RequestBody @ApiParam(name = "productMan",value = "商品管理的pojo信息",required = true) Lable lable){


        int i = lableService.LabeAdd(lable);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

    @PostMapping("/lable/state")
    @RequiresPermissions("5")
    @ApiOperation(value = "商品标签",notes = "用于修改商品标签状态",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map lableUpdateState(@RequestBody @ApiParam(name = "productMan",value = "商品管理的pojo信息",required = true) Lable lable){


        int i = lableService.updaeState(lable);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }
    @PostMapping("/comment/add")
    @RequiresPermissions("6")
    @ApiOperation(value = "商品评论接口",notes = "商品评论添加",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map commentInsert(@RequestBody @ApiParam(name = "comment",value = "系统用户的信息",required = true) Comment comment){
       commentService.commAdd(comment);
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/comment/findAll")
    @ApiOperation(value = "商品评论查找接口",notes = "查找所有的商品评论信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query")
    })
    public Map commentFindAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        Page<Object> objects = PageHelper.startPage(page, limit);

        List<Comment> comments = commentService.commFindAll();
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        jsonResult.put("count",commentPageInfo.getTotal());
        jsonResult.setData(commentPageInfo);
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/comment/findName")
    @ApiOperation(value = "商品评论根据名称查找接口",notes = "模糊查找名称的商品评论信息",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "查找该名称",required = true,dataType = "String",paramType = "query")
    })
    public Map commentFindName (@RequestParam("page") int page, @RequestParam("limit") int limit,@RequestParam("name") String name){
        Comment comment = new Comment();
        comment.setContent(name);
        Page<Object> startPage = PageHelper.startPage(page, limit);
        List<Comment> comments = commentService.commFindName(comment);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        jsonResult.put("count",commentPageInfo.getTotal());
        jsonResult.setData(commentPageInfo.getList());
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    @PostMapping("/comment/del")
    @ApiOperation(value = "商品评论删除接口",notes = "根据评论的id删除",httpMethod = "post", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "评论的id删除",required = true,dataType = "int",paramType = "query")
    })
    public Map commentDel(@RequestParam("id") int id){

        Comment comment = new Comment();
        comment.setId(id);
        int i = commentService.commDelete(comment);
        if (i==1){
            jsonResult.setCode("200");
        }else {
            jsonResult.setCode("0");
        }

        return  jsonResult.getValues();
    }

    @PostMapping("/comment/stateAndRecd")
    @ApiOperation(value = "商品评论接口",notes = "修改评论状态（false/隐藏，true/不隐藏）和评论推荐（false/不推荐，true/推荐）",httpMethod = "post", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map commentStateAndRecd  (@RequestBody @ApiParam(name = "comment",value = "Comment数据报表的信息",required = true) Comment comment){

//        com_recd	推荐
//        com_state	状态

        commentService.commStateAndRecd(comment);
        jsonResult.setCode("200");
        return  jsonResult.getValues();
    }

    /**
     * 根据系统编号查询商品信息
     * @param sysnum 商品系统编号
     * @return
     * @author Fanjiang
     */
    @PostMapping("/findsysnum")
    public Map findBysysnum(@RequestParam("sysnum") String sysnum){
        Product sysNum = productService.findSysNum(sysnum);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(sysNum);
        return jsonResult.getValues();
    }



}
